//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

public class Navigator {
    public static final String TAG = "Navigator";
    private static Context sContext;
    private Navigator.Builder builder;
    public static final String PAGE_NAME_FROM_URL = "navigator_param_page_name_from_origin_url";
    public static final String PARAM_ORIGIN_URL = "navigator_param_origin_url";
    public static final String NATIVE_SCHEMA = "";
    public static final String NATIVE_HOST = "page";

    private static String mSchema = NATIVE_SCHEMA;
    private static String mHost = NATIVE_HOST;
    private final String mUrl;
    private final Uri mUri;

    public static void init(Context context) {
        init(context, NATIVE_SCHEMA, NATIVE_HOST);
    }

    public static void init(Context context, String schema) {
        init(context, schema, NATIVE_HOST);
    }

    public static void init(Context context, String schema, String host) {
        sContext = context;
        mSchema = schema;
        mHost = host;
    }

    Navigator(Uri uri) {
        this.mUri = uri;
        this.mUrl = uri.toString();
    }

    public boolean open() {
        if (this.builder != null) {
            Object context;
            if (this.builder.mActivity != null) {
                context = this.builder.mActivity;
            } else {
                context = sContext;
            }

            Nav nav = Nav.from((Context)context);
            if (this.builder.mRequestCode != -1) {
                nav.forResult(this.builder.mRequestCode);
            } else if (this.builder.mLaunchFlag != 0) {
                nav.withFlags(this.builder.mLaunchFlag);
            }

            if (this.builder.mUriBuilder != null) {
                try {
                    Uri e = this.mUri;
                    if ("http".equals(e.getScheme()) || "https".equals(e.getScheme())) {
                        android.net.Uri.Builder builder = Uri.parse("teachin://page/global_web").buildUpon();
                        builder.appendQueryParameter("url", this.mUrl);
                        e = builder.build();
                    }

                    return nav.withExtras(bundleFor(e)).toUri(e);
                } catch (Throwable var5) {
                    var5.printStackTrace();
                }
            }
        }

        return false;
    }

    private static Bundle bundleFor(Uri uri) {
        Bundle bundle = new Bundle();
        if (uri == null) {
            return bundle;
        } else {
            Set keys = uri.getQueryParameterNames();
            if (keys != null) {
                Iterator path = keys.iterator();

                while (path.hasNext()) {
                    String paths = (String)path.next();
                    String value = uri.getQueryParameter(paths);
                    if (value != null) {
                        bundle.putString(paths, value);
                    }
                }
            }

            String path1 = "";
            List paths1 = uri.getPathSegments();
            if (paths1 != null && paths1.size() > 0) {
                path1 = (String)paths1.get(0);
            }

            bundle.putString("navigator_param_page_name_from_origin_url", path1);
            bundle.putString("navigator_param_origin_url", uri.toString());
            return bundle;
        }
    }

    public String getUrl() {
        return this.mUrl;
    }

    public void setBuilder(Navigator.Builder builder) {
        this.builder = builder;
    }

    public static class Builder {
        private int mContainerId;
        private int mEnterAnimRes;
        private int mPopExitAnimRes;
        private int mLaunchFlag = 268435456;
        private int mRequestCode = -1;
        private Activity mActivity;
        private android.net.Uri.Builder mUriBuilder;
        private boolean mInvokedBuild = false;

        private String mHoast;

        public Builder() {
        }

        private void checkPageIsAlreadySet() {
            if (this.mUriBuilder == null) {
                throw new IllegalAccessError("先调setPageName，才能addParameter");
            }
        }

        private void checkNotInvokeBuild() {
            if (this.mInvokedBuild) {
                throw new IllegalAccessError("设完参数，再调build()");
            }
        }

        public Navigator.Builder addParameter(Map<String, String> map) {
            if (map == null || map.size() == 0) {
                return this;
            }
            this.checkNotInvokeBuild();
            this.checkPageIsAlreadySet();
            for (Entry<String, String> entry : map.entrySet()) {
                this.mUriBuilder.appendQueryParameter(entry.getKey(), entry.getValue());
            }
            return this;
        }

        public Navigator.Builder addParameter(@NonNull String key, @NonNull String value) {
            this.checkNotInvokeBuild();
            this.checkPageIsAlreadySet();
            this.mUriBuilder.appendQueryParameter(key, value);
            return this;
        }

        public Navigator.Builder addParameter(@NonNull String key, @NonNull Number value) {
            this.checkNotInvokeBuild();
            this.checkPageIsAlreadySet();
            this.mUriBuilder.appendQueryParameter(key, value.toString());
            return this;
        }

        public Navigator.Builder addParameter(@NonNull String key, @NonNull Boolean value) {
            this.checkNotInvokeBuild();
            this.checkPageIsAlreadySet();
            this.mUriBuilder.appendQueryParameter(key, value.toString());
            return this;
        }

        public Navigator.Builder setPageName(@NonNull String pageName) {
            this.checkNotInvokeBuild();
            this.mUriBuilder = Uri.parse(mSchema + "://" + mHost + "/" + pageName).buildUpon();
            return this;
        }

        public Navigator.Builder setPageName(String host, @NonNull String pageName) {
            this.checkNotInvokeBuild();
            this.mUriBuilder = Uri.parse(mSchema + "://" + host + "/" + pageName).buildUpon();
            return this;
        }

        public Navigator.Builder withUrl(@NonNull String fullUrl) {
            this.checkNotInvokeBuild();
            if (TextUtils.isEmpty(fullUrl)) {
                return new NullBuilder();
            } else {
                this.mUriBuilder = Uri.parse(fullUrl).buildUpon();
                return this;
            }
        }

        public Navigator.Builder setContainerId(int containerId) {
            this.checkNotInvokeBuild();
            this.mContainerId = containerId;
            return this;
        }

        public Navigator.Builder setEnterAnimRes(int enterAnimRes) {
            this.checkNotInvokeBuild();
            this.mEnterAnimRes = enterAnimRes;
            return this;
        }

        public Navigator.Builder setPopExitAnimRes(int popExitAnimRes) {
            this.checkNotInvokeBuild();
            this.mPopExitAnimRes = popExitAnimRes;
            return this;
        }

        public Navigator.Builder setLaunchFlag(int launchFlag) {
            this.checkNotInvokeBuild();
            this.mLaunchFlag = launchFlag;
            return this;
        }

        public Navigator.Builder setRequestCode(Fragment fragment, int requestCode) {
            this.checkNotInvokeBuild();
            if (fragment != null && fragment.getActivity() != null) {
                this.mActivity = fragment.getActivity();
                this.mRequestCode = requestCode;
                return this;
            } else {
                throw new NullPointerException("the Fragment and Activity must not null");
            }
        }

        public Navigator.Builder setRequestCode(Activity activity, int requestCode) {
            this.checkNotInvokeBuild();
            if (activity == null) {
                throw new NullPointerException("the Activity mast not null");
            } else {
                this.mActivity = activity;
                this.mRequestCode = requestCode;
                return this;
            }
        }

        public Navigator build() {
            this.mInvokedBuild = true;
            Uri uri = this.mUriBuilder.build();

            Navigator navigator = new Navigator(uri);
            navigator.setBuilder(this);
            return navigator;
        }
    }
}
